<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\logic;



use app\common\model\ChatList;
use app\common\model\Customer;


use app\common\model\OtcCustomer;
use app\common\model\OtcOrder;
use GatewayClient\Gateway;
use think\Db;

/**
 * 用户接口逻辑
 */
class OtcService extends ApiBase
{


    /***
     * OTC聊天记录
     */

    public function chat_log($data){

        $member=$this->logicApiBase->member;
        $uid=$member['id'];
        $order=OtcOrder::get($data['id']);
        OtcCustomer::update(['is_read'=>1],['user_id'=>$uid,'otc_order_id'=>$order['id']]);
        $list=$this->modelOtcCustomer->getList(['otc_order_id'=>$order['id']],'*','id desc');
        foreach ($list as $k =>$v){
            $list[$k]['user_image']=$v->user->avtar;
            unset($v->user);
        }
        return $list;
    }

    /***
     * 检测对方是否在线
     */
    public function check_customer($data){
        $member=$this->logicApiBase->member;
        $uid=$member['id'];
        $order=OtcOrder::get($data['id']);
        Gateway::$registerAddress = getConfig('websocket_ip').':1236';
        if($order['uid']==$uid){
            $mem_id=$order['serial_number'].'_'.$order['user_id'];
        }else{
            $mem_id=$order['serial_number'].'_'.$order['uid'];
        }
        $status=Gateway::isUidOnline($mem_id);
        return $status;
    }


    /***
     * 绑定用户id
     */
    public function blind_user($data){

        $client_id = $data["client_id"];
        $member=$this->logicApiBase->member;
        $uid=$member['id'];
        $order=OtcOrder::get($data['id']);
        Gateway::$registerAddress = getConfig('websocket_ip').':1236';
        $mem_id=$order['serial_number'].'_'.$uid;
        Gateway::bindUid($client_id,$mem_id);
        OtcCustomer::where(['user_id'=>$uid,'otc_order_id'=>$data['id']])->update(['is_read'=>1]);
        return ['mem_id'=>$mem_id];

    }




    /**
     * 发送消息
     */
    public function  chat_message($data){
        $member=$this->logicApiBase->member;
        $uid=$member['id'];
        $order=OtcOrder::get($data['id']);
        Gateway::$registerAddress = getConfig('websocket_ip').':1236';
        if($order['uid']==$uid){
            $mem_id=$order['serial_number'].'_'.$order['user_id'];
            $user_id=$order['user_id'];
        }else{
            $mem_id=$order['serial_number'].'_'.$order['uid'];
            $user_id=$order['uid'];
        }
        if($data['type']==2){
            $content=get_picture_url( $data['content']) ;
        }else{
            $content = $data['content'];
        }
        Db::startTrans();
        try{
            //$res = var_export(Gateway::getClientIdByUid($friend_id));


            $message= [
                'type'=>$data['type'],
                'uid'=>$uid,
                'user_image'=>$member->avtar,
                'message'=>$content,
                'time'=>date('Y-m-d H:i:s')
            ];
            $message = json_encode($message);
            $status=Gateway::isUidOnline($mem_id);
            if($status){
                //发送消息
                Gateway::sendToUid($mem_id, $message);

            }
            $arr=[
                'content'=> $data['content'],
                'user_id'=>$user_id,
                'uid'=>$uid,
                'type'=>$data['type'],
                'otc_order_id'=>$data['id'],
            ];
            OtcCustomer::create($arr);
                // 提交事务
            Db::commit();
            return ['avtar'=>$member->avtar,'time'=>date('Y-m-d H:i:s'),'mem_id'=>$mem_id];
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ajaxreturn(1,$e->getMessage());
            return ajaxreturn(1,lang('可能由于网络原因'));
        }
    }





}
